perf(knn): reduce memory for batch flat vector search#6948
Closed
LeoReeYang wants to merge 1 commit into
Closed
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Avoid retaining whole scan RecordBatches (and vectors) in batch flat KNN heaps when the final projection does not include the vector column. When vectors are requested, retain only a per-row copy.
LeoReeYang
force-pushed
the
cursor/b7318f3e
branch
from
May 26, 2026 22:09
3e1bfb2 to
1226b70
Compare
Contributor
Author
|
Superseded by #6950 after branch rename to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
RecordBatches (and the full vector column) in the commoncolumns=[...]case.Implementation notes
Scanner::flat_knncomputesretain_vectorfrom the final projection and plumbs it intoKnnBatchParams.KNNVectorDistanceExecbatch mode uses a two-tier candidate representation:RowIdOnly { query_index, distance, row_id }whenretain_vector=falseWithVector { ..., slim_batch, row_index, vector_row }whenretain_vector=trueMemory (unit test)
Synthetic workload matching
test_batch_knn_heap_memory:num_rows=4096,dim=512,m=10,k=10.What is measured: sum of Arrow
get_array_memory_size()over candidate heap entries (not peak RSS or end-to-end query memory).< baseline/50)< baseline/10)Notes:
m×kcandidates holds a shallow clone of the full scan batch (vector +_rowidfor allnum_rows). In this synthetic setup the same batch is cloned repeatedly, so the counter sums shared buffers per candidate (worst-case retention model).WithVectoris on the order ofm×k×d×4(= 204,800 bytes for vector data) plus one deduplicated slim batch (_rowidonly).Latency (local, secondary)
Release build (
uv run maturin develop --uv --release),use_index=false,with_row_id=true.Workload:
rows=200_000,dim=256,m=10,k=10(medians over 8 rounds, 2 warmup).upstream-main)Batch latency improved ~1.71× vs baseline in this workload; separate-query path unchanged.
Test plan
cargo test -p lance --lib batch_knncargo clippy -p lance --tests -- -D warningscd python && uv run pytest python/tests/test_vector_index.py -k batch